﻿// ===================================
// <copyright>LogoUI Co.</copyright>
// <author>Vlad Spivak</author>
// <email>mailto:vlads@logoui.co.il</email>
// <created>21/00/10</created>
// <lastedit>Tuesday, November 23, 2010</lastedit>

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
// <remarks>Part of this software based on various internet sources, mostly on the works
// of members of Wpf Disciples group http://wpfdisciples.wordpress.com/
// Also project may contain code from the frameworks: 
//        Nito 
//        OpenLightGroup
//        nRoute
// </remarks>
// ====================================================================================//


using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

namespace LogoFX.Infra.Interactivity
{
    /// <summary>
    /// run storyboards on hover
    /// </summary>
    public class HoverBehavior : Behavior<FrameworkElement>
    {
        #region EnterStoryboard dependency property

        public Storyboard EnterStoryboard
        {
            get { return (Storyboard)GetValue(EnterStoryboardProperty); }
            set { SetValue(EnterStoryboardProperty, value); }
        }

        public static readonly DependencyProperty EnterStoryboardProperty =
            DependencyProperty.Register("EnterStoryboard", typeof(Storyboard), typeof(HoverBehavior), new PropertyMetadata(default(Storyboard), OnEnterStoryboardChanged));

        private static void OnEnterStoryboardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoverBehavior hb = (HoverBehavior)d;
            AttachEnter(hb);
        }

        private static void AttachEnter(HoverBehavior hb)
        {
            if (hb.EnterStoryboard != null && hb.AssociatedObject != null)
                hb.EnterStoryboard.Children.Where(b => String.IsNullOrWhiteSpace(Storyboard.GetTargetName(b))).Apply(c => Storyboard.SetTarget(c, hb.AssociatedObject));
        }

        #endregion

        #region LeaveStoryboard dependency property

        public Storyboard LeaveStoryboard
        {
            get { return (Storyboard)GetValue(LeaveStoryboardProperty); }
            set { SetValue(LeaveStoryboardProperty, value); }
        }

        public static readonly DependencyProperty LeaveStoryboardProperty =
            DependencyProperty.Register("LeaveStoryboard", typeof(Storyboard), typeof(HoverBehavior), new PropertyMetadata(default(Storyboard), OnLeaveStoryboardChanged));

        private static void OnLeaveStoryboardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoverBehavior hb = (HoverBehavior)d;
            AttachLeave(hb);
        }

        private static void AttachLeave(HoverBehavior hb)
        {
            if (hb.LeaveStoryboard != null && hb.AssociatedObject != null)
                hb.LeaveStoryboard.Children.Where(b => String.IsNullOrWhiteSpace(Storyboard.GetTargetName(b))).Apply(c => Storyboard.SetTarget(c, hb.AssociatedObject));
        }

        #endregion

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseEnter += MouseEnter;
            AssociatedObject.MouseLeave += MouseLeave;
            AttachEnter(this);
            AttachLeave(this);
        }


        private void MouseEnter(object sender, MouseEventArgs e)
        {
            if (LeaveStoryboard != null)
                LeaveStoryboard.Stop();
            if (EnterStoryboard != null)
                EnterStoryboard.Begin();
        }

        private void MouseLeave(object sender, MouseEventArgs e)
        {
            if (EnterStoryboard != null)
                EnterStoryboard.Stop();            
            if (LeaveStoryboard != null)
                LeaveStoryboard.Begin();
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseEnter -= MouseEnter;
            AssociatedObject.MouseLeave -= MouseLeave;
            base.OnDetaching();
        }
    }
}
